'use strict';
app.controller('agentAreaAddCtrl', ['$scope', '$modalInstance', '$state', 'areaService', 'agentAreaService', 'toaster',
    function($scope, $modalInstance, $state, areaService, agentAreaService, toaster) {

    $scope.agentArea = {
        name: '',
        provinceId: 0,
        cityId: 0,
        districtIds: []
    }

    //获取还未建立代理区域的省份列表
    $scope.provinceSelectList=[];
    $scope.getProvinceList = function(){
        areaService.getAvailableProvinces($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.provinceSelectList = result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
    $scope.getProvinceList();

    //根据省级Id获取旗下未建立代理区域的城市列表
    $scope.citySelectList=[];
    $scope.upCitys = function(){
        if($scope.agentArea.provinceId == null
        || $scope.agentArea.provinceId.length <= 0) {
            $scope.citySelectList=[];
            $scope.districtSelectList=[];
            $scope.agentArea.cityId = 0;
            $scope.agentArea.districtIds = [];
            return;
        }

        areaService.getAvailableCitys($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.citySelectList = result.data;
                    $scope.agentArea.cityId = $scope.citySelectList[0].cityId;
                    $scope.upDistricts();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    //根据市级id获取旗下未建立代理区域的区/县列表
    $scope.districtSelectList=[];
    $scope.upDistricts = function(){
        areaService.getAvailableDistricts($scope).then(function (result) {
            $scope.agentArea.districtIds = [];
            switch (result.status) {
                case 200:
                    $scope.districtSelectList = result.data;
                    for(var index in $scope.districtSelectList) {
                        var district = $scope.districtSelectList[index];
                        $scope.agentArea.districtIds[district.id] = false;
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    // 提交
    $scope.AllowToAdd = true;
    $scope.add = function(){
        if(!$scope.AllowToAdd){
            return;
        }
        $scope.AllowToAdd=false;

        var isChecked = false;
        for(var id in $scope.agentArea.districtIds) {
            isChecked = $scope.agentArea.districtIds[id];
            if(isChecked) {
                break;
            }
        }

        if(!isChecked) {
            toaster.pop('waiting', "提示", '请选勾选区/县城市');
            $scope.AllowToAdd=true;
            return;
        }

        if($scope.agentArea.name.length <= 0) {
            toaster.pop('waiting', "提示", '请填写代理区域名称');
            $scope.AllowToAdd=true;
            return;
        }

        agentAreaService.add($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.cancel();
                    toaster.pop('success', "提示", result.data.message);
                    $scope.AllowToAdd=true;
                    $state.reload('app.agentArea');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    $scope.AllowToAdd=true;
                    break;
            }
        });
    }

    $scope.cancel = function () {
        $modalInstance.dismiss('close');
    };
}]);